/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.config.RentApi;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.model.RentInsurance;
import com.ybkj.daijia.server.service.RentInsuranceService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * @author Shine
 *
 */
@Service
public class DefaultRentInsuranceService implements RentInsuranceService {


    @Autowired
    private RentApi rentApi;

    @Autowired
    private SettingUtils settingUtils;

    public Map<String, Object> insertInsurance(Long carId, String company,
        String insuranceNo, String insuranceType, Double money,
        Integer validDate, Integer invalidDate) throws Exception {

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("carId", String.valueOf(carId));
        kv.put("company", company);
        kv.put("insuranceNo", insuranceNo);
        kv.put("insuranceType", insuranceType);
        kv.put("money", String.valueOf(money));
        kv.put("validDate", String.valueOf(validDate));
        kv.put("invalidDate", String.valueOf(invalidDate));
        String result = rentApi
            .get(rentApi.getApiBaseURL() + rentApi.INSERT_RENTINSURANCE, kv, settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        Map<String, Object> map = new HashMap<String, Object>();
        if (code == 0) {
            map.put("message", "创建成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }


    @Override
    public RentInsurance findInsuranceById(Long id) {

        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(rentApi.FIND_RENTINSURANCE_BY_ID)
            .append("?id={id}&appkey={appkey}");

        Map<String, String> kv = new HashMap<String, String>();
        kv.put("id", String.valueOf(id));
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().create();
                RentInsurance rentInsurance = gson.fromJson(data.toString(), RentInsurance.class);
                return rentInsurance;
            }
        }
        return null;
    }

    @Override
    public Map<String, Object> deleteInsuranceByCarId(Long carId) {

        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(rentApi.DELETE_RENTINSURANCE_BY_CARID)
            .append("?carId={carId}&appkey={appkey}");
        Map<String, String> kv = new HashMap<String, String>();
        kv.put("carId", String.valueOf(carId));
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        Map<String, Object> map = new HashMap<String, Object>();
        if (code == 0) {
            map.put("message", "删除成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @Override
    public Map<String, Object> updateInsurance(String company,
        String insuranceNo, String insuranceType, Double money,
        Integer validDate, Integer invalidDate) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();

        kv.add("company", company);
        kv.add("insuranceNo", insuranceNo);
        kv.add("insuranceType", insuranceType);
        kv.add("money", String.valueOf(money));
        kv.add("validDate", String.valueOf(validDate));
        kv.add("invalidDate", String.valueOf(invalidDate));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + rentApi.UPDATE_RENTINSURANCE, kv,
                settingUtils.get());

        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;

    }

    @Override
    public Page<RentInsurance> findInsuranceByCarId(Integer page, Integer rows, Long carId) {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());
        Map<String, String> kv = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(rentApi.FIND_RENTINSURANCE_BY_CARID)
            .append("?appkey={appkey}");
        page = Pager.getPage();   //每页的页数
        rows = Pager.getLimit();  //每页的条数
        if (rows != null) {
            kv.put("rows", String.valueOf(rows));
            builder.append("&rows={rows}");
        }
        if (page != null) {
            kv.put("page", String.valueOf(page + 1));
            builder.append("&page={page}");
        }
        if (carId != null) {
            kv.put("carId", String.valueOf(carId));
            builder.append("&carId={carId}");
        }
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        List<RentInsurance> rentInsurance = new ArrayList<RentInsurance>();
        long total = 0;
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");

            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                total = data.getLong("total");
                JSONArray jsonArray = data.getJSONArray("rows");
                Gson gson = new GsonBuilder().create();
                rentInsurance = gson
                    .fromJson(jsonArray.toString(), new TypeToken<List<RentInsurance>>() {
                    }.getType());

            }
        }
        return new PageImpl<RentInsurance>(rentInsurance, pageable, total);
    }


}